package com.zz.yt.lib.bluetooth.delegate.pad;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.zyapi.pos.PosManager;
import android.zyapi.pos.PrinterDevice;
import android.zyapi.pos.interfaces.OnPrintEventListener;
import android.zyapi.pos.utils.BitmapTools;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.EncodeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.util.log.LatteLogger;
import com.zz.yt.lib.ui.listener.OnClickStringListener;


/**
 * 打印工具
 *
 * @author qf
 * @version 1.0
 */
public class PrintingUtil implements OnPrintEventListener {

    public static final String BASE_JPEG_64 = "data:image/jpeg;base64,";

    private OnClickStringListener mListener = null;

    @NonNull
    public static PrintingUtil create(String base64Data) {
        return new PrintingUtil(base64Data);
    }

    /**
     * 打印base64的条形码
     */
    public PrintingUtil(String base64Data) {
        //浓度 12-50
        final int concentration = 22;
        //图片的距离纸张的左边的距离（8-40）
        final int left = 11;

        Bitmap mBitmap = null;
        try {
            byte[] bytes = EncodeUtils.base64Decode(base64Data.replace(BASE_JPEG_64, ""));
            mBitmap = ConvertUtils.bytes2Bitmap(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mBitmap == null) {
            LatteLogger.e("图片转换出错");
            return;
        }
        //宽度缩放
        mBitmap = zoomImg(mBitmap, 345);
        final PrinterDevice mPrinter = PosManager.get().getPrinterDevice();
        if (mPrinter == null) {
            LatteLogger.e("PrinterDevice未拿到");
            return;
        }
        mPrinter.setPrintEventListener(this);
        mPrinter.init();
        //对图像二值化
        final Bitmap bitmap = BitmapTools.gray2Binary(mBitmap);
        mPrinter.addBmp(concentration, left, bitmap);
        mPrinter.printStart();
    }

    public void setOnClickListener(OnClickStringListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void onEvent(int i) {
        switch (i) {
            case EVENT_UNKNOW:
                ToastUtils.showShort("未知错误");
                break;
            case EVENT_NO_PAPER:
                ToastUtils.showShort("打印机缺纸");
                break;
            case EVENT_PAPER_JAM:
                ToastUtils.showShort("打印机卡纸");
                break;
            case EVENT_PRINT_OK:
                LatteLogger.i("打印成功");
                if (mListener != null) {
                    mListener.onClick(null);
                }
                break;
            case EVENT_HIGH_TEMP:
                ToastUtils.showShort("机芯温度过热");
                break;
            case EVENT_LOW_TEMP:
                ToastUtils.showShort("机芯温度过低");
                break;
            case EVENT_CONNECTED:
                ToastUtils.showShort("打印机连接完成");
                break;
            case EVENT_CONNECT_FAILD:
                ToastUtils.showShort("打印机连接失败");
                break;
            case EVENT_STATE_OK:
                ToastUtils.showShort("打印机状态正常");
                break;
            case EVENT_CHECKED_BLACKFLAG:
                ToastUtils.showShort("检测到黑标");
                break;
            case EVENT_NO_CHECKED_BLACKFLAG:
                ToastUtils.showShort("未检测到黑标");
                break;
            case EVENT_TIMEOUT:
                ToastUtils.showShort("打印机响应超时");
                break;
            case EVENT_PRINT_FAILD:
                ToastUtils.showShort("打印失败");
                break;
            default:
                ToastUtils.showShort("打印失败:" + i);
                break;
        }
    }

    @Override
    public void onCheckBlack(int event) {
        switch (event) {
            case EVENT_CHECKED_BLACKFLAG:
                ToastUtils.showShort("检测到黑标");
                break;
            case EVENT_NO_CHECKED_BLACKFLAG:
                ToastUtils.showShort("没有检测到黑标");
                break;
            case EVENT_NO_PAPER:
                ToastUtils.showShort("检测黑标时缺纸");
                break;
            default:
                break;
        }
    }

    @Override
    public void onGetState(int i, int i1) {

    }

    public static Bitmap zoomImg(Bitmap bm, int newWidth) {
        Bitmap newbm = null;
        if (bm != null) {
            // 获得图片的宽高
            int width = bm.getWidth();
            int height = bm.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, 1);
            // 得到新的图片
            newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        }
        return newbm;
    }


    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        Bitmap newbm = null;
        if (bm != null) {
            // 获得图片的宽高
            int width = bm.getWidth();
            int height = bm.getHeight();
            // 计算缩放比例
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // 取得想要缩放的matrix参数
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            // 得到新的图片
            newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        }
        return newbm;
    }
}
